package cn.com.dhcc.turbo.service.exch.exhmgr;

import java.io.File;
import java.util.List;

import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.tag.CommonStaticCode.YESNO;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.dao.exch.FtpUserDao;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.FtpUser;

/**
 * Created by zss on 2014/5/15.
 */
@Service
public class FtpUserService extends AbstractCrudService<FtpUser> {
	//用户上传到ftp服务器的xml目录。该目录是相对目录,存在用户自身目录下
	private static final String uploadxmlDir =AppConst.FILEDATA_UPLOADXML_DIR;
	//用户上传到ftp服务器的文件目录。该目录是相对目录,存在用户自身目录下
	private static final String uploadmetadataDir =AppConst.FILEDATA_UPLOADMETADATA_DIR;
	//插入数据库成功后xml响应的目录。该目录是相对目录，存在用户自身目录下
	private static final String successxmlDir = AppConst.FILEDATA_SUCCESSXML_DIR;
	//插入数据库失败后xml响应的目录。该目录是相对目录，存在用户自身目录下
	private static final String failxmlDir = AppConst.FILEDATA_FAILXML_DIR;
	//接收节点公共xml目录。admin目录下
	private static final String recvxmlDir = AppConst.FILEDATA_RECVXML_DIR;
	//接收节点公共文件目录。admin目录下
	private static final String recvmetadataDir = AppConst.FILEDATA_RECVMETADATA_DIR;
	//接收xml头文件目录。admin目录下
	private static final String remoterespDir =AppConst.FILEDATA_REMOTERESP_DIR;
	//接收xml头文件目录。admin目录下
	private static final String localrespDir =AppConst.FILEDATA_LOCALRESP_DIR;
	
	private static final String crossmetadataDir = AppConst.FILEDATA_CROSSMETADATA_DIR;
	
	//接收节点公共文件目录。admin目录下
	private static final String crossxmlDir =AppConst.FILEDATA_CROSSXML_DIR;
	@Autowired
	private FtpUserDao dao;

	@Override
	public CrudDao<FtpUser> getCrudDao() {
		return dao;
	}

	public FtpUser getFtpadmin() throws ServiceException {
		try {
			return dao.getFtpadmin();
		} catch (DaoException e) {
			throw new ServiceException("获取FtpUser用户列表失败", e);
		}
	}

	public List<FtpUser> getAllFtpUser() throws ServiceException {
		try {
			return dao.getAllFtpUser();
		} catch (DaoException e) {
			throw new ServiceException("获取FtpUser用户列表失败", e);
		}
	}

	public void deleteFtpUserByAdapterCode(String adapterCode) throws ServiceException {
		//sysCode==id
		try {
			super.delete(adapterCode);
		} catch (ServiceException e) {
			throw new ServiceException("删除FtpUser用户列表失败", e);
		}
	}

	public boolean addFtpUser(FtpUser ftpUser) throws ServiceException {
		try {
			return dao.addFtpUser(ftpUser);
		} catch (DaoException e) {
			throw new ServiceException("添加FtpUser用户失败", e);
		}
	}

	/**
	 * 创建管理员账号
	 *
	 * @throws ServiceException
	 */
	public void createFtpAdmin() throws ServiceException {
		FtpUser adminUser = new FtpUser();
		adminUser.setUserid(AppConst.EMBFTP.EMBFTP_ADMIN_USERID);
		adminUser.setUserpassword(new SaltedPasswordEncryptor().encrypt(AppConst.EMBFTP.EMBFTP_ADMIN_PWD));
		String path = AppConst.EMBFTP.EMBFTP_ROOTDIR + File.separator + AppConst.EMBFTP.EMBFTP_ADMIN_USERID;
		mkadmindirs(path);
		adminUser.setHomedirectory(path);
		adminUser.setEnableflag(Integer.parseInt(YESNO.YES.getStatus()));
		adminUser.setIdletime(0l);
		adminUser.setWritepermission(Integer.parseInt(YESNO.YES.getStatus()));
		addFtpUser(adminUser);
	}


	private boolean mkdirs(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}

		File uploadxmlDirfile = new File(file, uploadxmlDir);
		if (!uploadxmlDirfile.exists()) {
			uploadxmlDirfile.mkdirs();
		}
		File uploadmetadataDirfile = new File(file, uploadmetadataDir);
		if (!uploadmetadataDirfile.exists()) {
			uploadmetadataDirfile.mkdirs();
		}
		File successxmlDirfile = new File(file, successxmlDir);
		if (!successxmlDirfile.exists()) {
			successxmlDirfile.mkdirs();
		}
		File failxmlDirfile = new File(file, failxmlDir);
		if (!failxmlDirfile.exists()) {
			failxmlDirfile.mkdirs();
		}
		
		
		File crossmetadataDirfile = new File(file, crossmetadataDir);
		if (!crossmetadataDirfile.exists()) {
			crossmetadataDirfile.mkdirs();
		}
		File crossxmlDirfile = new File(file, crossxmlDir);
		if (!crossxmlDirfile.exists()) {
			crossxmlDirfile.mkdirs();
		}
		return true;
	}

	private boolean mkadmindirs(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		File recvxmlDirfile = new File(file, recvxmlDir);
		if (!recvxmlDirfile.exists()) {
			recvxmlDirfile.mkdirs();
		}
		File recvmetadataDirfile = new File(file, recvmetadataDir);
		if (!recvmetadataDirfile.exists()) {
			recvmetadataDirfile.mkdirs();
		}
		File remoterespDirfile = new File(file, remoterespDir);
		if (!remoterespDirfile.exists()) {
			remoterespDirfile.mkdirs();
		}
		File localrespDirfile = new File(file, localrespDir);
		if (!localrespDirfile.exists()) {
			localrespDirfile.mkdirs();
		}
		return true;
	}

	
	/**
	 * 创建适配器账号
	 * @param adapterInfo
	 * @throws ServiceException
	 */
	public void createAdapterUser(AdapterInfo adapterInfo) throws ServiceException {
		FtpUser adapterUser =this.get(adapterInfo.getAdapterCode());//如果没有创建则创建，
		if (adapterUser==null) {
			adapterUser = new FtpUser();
			adapterUser.setUserid(adapterInfo.getAdapterCode());
			adapterUser.setUserpassword(new SaltedPasswordEncryptor().encrypt(adapterInfo.getAdapterCode()));
			String path = AppConst.EMBFTP.EMBFTP_ROOTDIR + File.separator + adapterInfo.getAdapterCode();
			mkdirs(path);
			adapterUser.setHomedirectory(path);
			adapterUser.setEnableflag(Integer.parseInt(YESNO.YES.getStatus()));
			adapterUser.setIdletime(0l);
			adapterUser.setWritepermission(Integer.parseInt(YESNO.YES.getStatus()));
			addFtpUser(adapterUser);
		}
	}

}
